package com.ruoyi.project.maintain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.project.maintain.domain.bo.ProjectMaintainBo;
import com.ruoyi.project.maintain.domain.vo.ProjectMaintainVo;
import com.ruoyi.project.maintain.domain.ProjectMaintain;
import com.ruoyi.project.maintain.mapper.ProjectMaintainMapper;
import com.ruoyi.project.maintain.service.IProjectMaintainService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工程维护Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-09
 */
@RequiredArgsConstructor
@Service
public class ProjectMaintainServiceImpl implements IProjectMaintainService {

    private final ProjectMaintainMapper baseMapper;

    /**
     * 查询工程维护
     */
    @Override
    public ProjectMaintainVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工程维护列表
     */
    @Override
    public TableDataInfo<ProjectMaintainVo> queryPageList(ProjectMaintainBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProjectMaintain> lqw = buildQueryWrapper(bo);
        Page<ProjectMaintainVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工程维护列表
     */
    @Override
    public List<ProjectMaintainVo> queryList(ProjectMaintainBo bo) {
        LambdaQueryWrapper<ProjectMaintain> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProjectMaintain> buildQueryWrapper(ProjectMaintainBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProjectMaintain> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), ProjectMaintain::getProjectName, bo.getProjectName());
        lqw.eq(bo.getCreateTime() != null, ProjectMaintain::getCreatetime , bo.getCreateTime());
        return lqw;
    }

    /**
     * 新增工程维护
     */
    @Override
    public ProjectMaintain insertByBo(ProjectMaintainBo bo) {
        ProjectMaintain add = BeanUtil.toBean(bo, ProjectMaintain.class);
        validEntityBeforeSave(add);
        add.setCreatetime(new Date());
        baseMapper.insert(add);
        return add;
    }

    /**
     * 修改工程维护
     */
    @Override
    public ProjectMaintain updateByBo(ProjectMaintainBo bo) {
        ProjectMaintain update = BeanUtil.toBean(bo, ProjectMaintain.class);
        validEntityBeforeSave(update);
        baseMapper.updateById(update);
        return update;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProjectMaintain entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工程维护
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
